/**
 *
 * Copyright 2016-present scrm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import * as types from '../constants/ActionTypes';

const initialState = {
  isRefreshing: false,
  loading: false,
  isLoadMore: false,
  noMore: false,
  teamList: null,
  type: null,
  editingTeam: null,
  team: null,
  callback: null,
  saveTeamRes: {}
};

export default function team(state = initialState, action) {
  switch (action.type) {
    case types.FETCH_TEAM_LIST:
      return Object.assign({}, state, {
        isRefreshing: action.isRefreshing,
        loading: action.loading,
        isLoadMore: action.isLoadMore,
        type: action.type
      });
    case types.RECEIVE_TEAM_LIST:
      return Object.assign({}, state, {
        type: action.type,
        isRefreshing: false,
        isLoadMore: false,
        noMore: action.teamList && action.teamList.length === 0,
        teamList: state.teamList
          ? loadMore(state, action)
          : (state.teamList === false ? null : combine(state, action)),
        loading: false
      });
    case types.RECEIVE_TEAM_DETAIL:
      return Object.assign({}, state, {
        team: action.team,
        type: action.type
      });
    case types.START_SAVE_TEAM:
      return Object.assign({}, state, {
        type: action.type,
        loading: action.loading
      });
    case types.RESPONSE_SAVE_TEAM:
      return Object.assign({}, state, {
        type: action.type,
        loading: action.loading,
        callback: JSON.stringify(action.response),
        saveTeamRes: action.response,
        editingTeam: action.team
      });
    case types.RESET_TEAM_TYPE:
      return Object.assign({}, state, {
        type: action.type,
        editingTeam: null
      });
    case types.EMPTY_TEAM_LIST: {
      return Object.assign({}, state, {
        teamList: action.teamList,
        loading: action.loading,
        editingTeam: action.editingTeam,
        isRefreshing: false,
        type: types.EMPTY_TEAM_LIST
      });
    }  
    default:
      return state;
  }
}

function combine(state, action) {
  state.teamList = action.teamList
  return state.teamList;
}

function loadMore(state, action) {
  state.teamList = concatFilterDuplicate(
    state.teamList,
    action.teamList
  );
  return state.teamList;
}

/**
 * filter duplicate data when loading more.
*/
function concatFilterDuplicate(list1, list2) {
  const set = new Set(list1.map(item => item.id));
  const filterList2 = [];
  const length = list2.length;
  for (let i = 0; i < length; i++) {
    if (!set.has(list2[i].id)) {
      filterList2.push(list2[i]);
    }
  }
  return list1.concat(filterList2);
}
